#include "NewBuilder.h"

#include "ScriptCompiler/ASTBuilder/TypeBuilder.h"
#include "ScriptCompiler/ScriptFileContext.h"
#include "ScriptCompiler/AST/NewNode.h"
#include "ScriptCompiler/ASTBuilder/ArgumentsBuilder.h"

CNewBuilder::CNewBuilder(std::shared_ptr<CScriptFileContext> InContext)
    : Context(InContext)
{
    Context = InContext;
    Result = std::make_shared<CNewNode>(InContext);
}

bool CNewBuilder::ReadCurentToken()
{
    //read 'new' keyword
    {
        auto Token = Context->GetCurrentToken();
        if(Token->Type != EScriptTokenType::New)
        {
            Context->RaiseError(10072,Token, U"Expect 'new' here");
            return false;
        }
        Context->CurrentTokenIndex++;

        Result->New= Token;
    }

    //read type
    {
        auto TypeBuilder = std::make_shared<CTypeBuilder>(Context);
        if(!TypeBuilder->ReadCurentToken())
        {
            return false;
        }
        Result->Type = TypeBuilder->Result;
    }



    //if there is not a '(' return true
    {
        auto Token = Context->GetCurrentToken();
        if(Token->Type != EScriptTokenType::LeftParentheses)
        {
            return true;
        }
    }



    //read arguments
    {
        auto ArgumentsBuilder = std::make_shared<CArgumentsBuilder>(Context);
        if(!ArgumentsBuilder->ReadCurentToken())
        {
            return false;
        }
        Result->Arguments = ArgumentsBuilder->Result;
    }

    return true;

}